<?php
namespace database;

use sql\SqlExec;
use sql\SqlWhereMerge;
use utils\PathTool;
use model\FileInf;
use database\DbHelper;
use PDO;
/*
	说明：
		1.在调用此函数前不能有任何输出操作。比如 echo print
		
	更新记录：
		2012-04-03 创建
		2014-08-11 更新数据库操作代码。 
		2017-05-31 精简代码
*/
class DBFile
{	
	function __construct() 
	{
	}
	static function Clear()
	{
		$db = new DbHelper();
		$db->exes(array("delete from up7_files;"
				,"delete from up7_folders;"));
	}	

	static function merged($id)
	{
		$sql = "update up7_files set f_lenSvr=f_lenLoc,f_perSvr='100%',f_merged=1 where f_id=:f_id";
		$db = new DbHelper();
		$cmd =& $db->GetCommand($sql);
	
		$cmd->bindParam(":f_id", $id);
		$db->ExecuteNonQuery($cmd);
	}
	
	static function read(&$m)
	{
		$ret = false;
		$sql = "select
		 		 f_pathSvr
		 		,f_pathRel
		 		,f_md5
		 		,f_lenLoc
		 		,f_sizeLoc
				,f_pos
				,f_lenSvr
				,f_pid
				,f_perSvr
				,f_complete
		 		,f_time
		 		,f_deleted
		 		,f_scan
		 		,f_pidRoot
		 		,f_fdTask
				,f_fdChild
				,f_uid
				,f_nameLoc
				,f_nameSvr
				,f_pathLoc
				 from up7_files where f_id=:f_id limit 1";
		
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$cmd->bindParam(":f_id", $m->id);
		$row = $db->ExecuteRow($cmd);
		
		if (!empty($row))
		{
			$m->pathSvr 		= $row["f_pathSvr"];
			$m->pathRel 		= $row["f_pathRel"];
			$m->md5 			= $row["f_md5"];
			$m->lenLoc 			= intval($row["f_lenLoc"]);
			$m->sizeLoc 		= $row["f_sizeLoc"];
			$m->lenSvr 			= intval($row["f_lenSvr"]);
			$m->pid 			= $row["f_pid"];
			$m->perSvr 			= $row["f_perSvr"];
			$m->complete 		= (bool)$row["f_complete"];
			$m->PostedTime 		= $row["f_time"];
			$m->deleted 		= (bool)$row["f_deleted"];
			//$m->merged 		= (bool)$row["f_merged"];
			$m->pidRoot 		= $row["f_pidRoot"];
			$m->fdTask 			= (bool)$row["f_fdTask"];
			$m->f_fdChild 		= (bool)$row["f_fdChild"];
			$m->uid 			= $row["f_uid"];
			$m->nameLoc 		= $row["f_nameLoc"];
			$m->nameSvr 		= $row["f_nameSvr"];
			$m->pathLoc 		= $row["f_pathLoc"];
			$ret = true;
		}
		return $ret;
	}
	
	static function read2($id)
	{
	    $sql = "select
                 f_id
		 		,f_pathSvr
		 		,f_pathRel
		 		,f_md5
		 		,f_lenLoc
		 		,f_sizeLoc
				,f_pos
				,f_lenSvr
				,f_pid
				,f_perSvr
				,f_complete
		 		,f_time
		 		,f_deleted
		 		,f_scan
		 		,f_pidRoot
		 		,f_fdTask
				,f_fdChild
				,f_uid
				,f_nameLoc
				,f_nameSvr
				,f_pathLoc
				 from up7_files where f_id=:f_id limit 1";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sql);
	    $cmd->bindParam(":f_id", $id);
	    $row = $db->ExecuteRow($cmd);
	    
	    $inf = null;
	    if (!empty($row))
	    {
	        $inf = new FileInf();
	        $inf->id            = $row["f_id"];
	        $inf->pathSvr 		= $row["f_pathSvr"];
	        $inf->pathRel 		= $row["f_pathRel"];
	        $inf->md5 			= $row["f_md5"];
	        $inf->lenLoc 		= intval($row["f_lenLoc"]);
	        $inf->sizeLoc 		= $row["f_sizeLoc"];
	        $inf->lenSvr 		= intval($row["f_lenSvr"]);
	        $inf->pid 			= $row["f_pid"];
	        $inf->perSvr 		= $row["f_perSvr"];
	        $inf->complete 		= (bool)$row["f_complete"];
	        $inf->PostedTime 	= $row["f_time"];
	        $inf->deleted 		= (bool)$row["f_deleted"];
	        $inf->pidRoot 		= $row["f_pidRoot"];
	        $inf->fdTask 		= (bool)$row["f_fdTask"];
	        $inf->fdChild 	    = (bool)$row["f_fdChild"];
	        $inf->uid 			= $row["f_uid"];
	        $inf->nameLoc 		= $row["f_nameLoc"];
	        $inf->nameSvr 		= $row["f_nameSvr"];
	        $inf->pathLoc 		= $row["f_pathLoc"];
	    }
	    return $inf;
	}
	
	static function read3($pathRel,$id,$uid)
	{
	    $sql = "select
                 f_id
		 		,f_pathSvr
		 		,f_pathRel
		 		,f_md5
		 		,f_lenLoc
		 		,f_sizeLoc
				,f_pos
				,f_lenSvr
				,f_pid
				,f_perSvr
				,f_complete
		 		,f_time
		 		,f_deleted
		 		,f_scan
		 		,f_pidRoot
		 		,f_fdTask
				,f_fdChild
				,f_uid
				,f_nameLoc
				,f_nameSvr
				,f_pathLoc
				 from up7_files where f_pathRel=:pathRel and f_deleted=0 and f_uid=:uid and f_id!=:f_id limit 1";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sql);
	    $cmd->bindParam(":pathRel", $pathRel);
	    $cmd->bindParam(":uid", $uid);
	    $cmd->bindParam(":f_id", $id);
	    $row = $db->ExecuteRow($cmd);
	    
	    $inf = null;
	    if (!empty($row))
	    {
	        $inf = new FileInf();
	        $inf->id            = $row["f_id"];
	        $inf->pathSvr 		= $row["f_pathSvr"];
	        $inf->pathRel 		= $row["f_pathRel"];
	        $inf->md5 			= $row["f_md5"];
	        $inf->lenLoc 		= intval($row["f_lenLoc"]);
	        $inf->sizeLoc 		= $row["f_sizeLoc"];
	        $inf->lenSvr 		= intval($row["f_lenSvr"]);
	        $inf->pid 			= $row["f_pid"];
	        $inf->perSvr 		= $row["f_perSvr"];
	        $inf->complete 		= (bool)$row["f_complete"];
	        $inf->PostedTime 	= $row["f_time"];
	        $inf->deleted 		= (bool)$row["f_deleted"];
	        $inf->pidRoot 		= $row["f_pidRoot"];
	        $inf->fdTask 		= (bool)$row["f_fdTask"];
	        $inf->fdChild 	    = (bool)$row["f_fdChild"];
	        $inf->uid 			= $row["f_uid"];
	        $inf->nameLoc 		= $row["f_nameLoc"];
	        $inf->nameSvr 		= $row["f_nameSvr"];
	        $inf->pathLoc 		= $row["f_pathLoc"];
	    }
	    return $inf;
	}
	
	static function complete($fid)
	{
		$sql = "update up7_files set f_lenSvr=f_lenLoc,f_perSvr='100%',f_complete=1,f_scan=1 where f_id=:f_id";
		
		$db = new DbHelper();
		$cmd =& $db->GetCommand($sql);
		
		$cmd->bindParam(":f_id", $fid);
		$db->ExecuteNonQuery($cmd);
	}
	
	/// <summary>
	/// 根据文件MD5获取文件信息
	/// 取已上传完的文件
	/// </summary>
	/// <param name="md5"></param>
	/// <param name="inf"></param>
	/// <returns></returns>
	function exist_file($md5, &$inf/*xdb_files*/)
	{
	    $ret = false;
	    $sb = "select * from (select ";
	    $sb = $sb . "f_id";
	    $sb = $sb . ",f_uid";
	    $sb = $sb . ",f_nameLoc";
	    $sb = $sb . ",f_nameSvr";
	    $sb = $sb . ",f_pathLoc";
	    $sb = $sb . ",f_pathSvr";
	    $sb = $sb . ",f_pathRel";
	    $sb = $sb . ",f_lenLoc";
	    $sb = $sb . ",f_sizeLoc";
	    $sb = $sb . ",f_pos";
	    $sb = $sb . ",f_lenSvr";
	    $sb = $sb . ",f_perSvr";
	    $sb = $sb . ",f_complete";
	    $sb = $sb . ",f_time";
	    $sb = $sb . ",f_deleted";
	    $sb = $sb . " from up7_files";
	    $sb = $sb . " where f_md5=:f_md5";
	    $sb = $sb . " and f_complete=1";
	    $sb = $sb . " order by f_lenSvr desc";
	    $sb = $sb . " ) tmp limit 1";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sb);
	    
	    $cmd->bindParam(":f_md5", $md5);
	    $row = $db->ExecuteRow($cmd);
	    
	    if (!empty($row))
	    {
	        $inf->id 			= $row["f_id"];
	        $inf->uid 			= $row["f_uid"];
	        $inf->nameLoc 		= $row["f_nameLoc"];
	        $inf->nameSvr 		= $row["f_nameSvr"];
	        $inf->pathLoc 		= $row["f_pathLoc"];
	        $inf->pathSvr 		= $row["f_pathSvr"];
	        $inf->pathRel 		= $row["f_pathRel"];
	        $inf->md5 			= $md5;
	        $inf->lenLoc 		= intval($row["f_lenLoc"]);
	        $inf->sizeLoc 		= $row["f_sizeLoc"];
	        $inf->blockOffset 	= intval($row["f_pos"]);
	        $inf->lenSvr 		= intval($row["f_lenSvr"]);
	        $inf->perSvr 		= $row["f_perSvr"];
	        $inf->complete 		= (bool)$row["f_complete"];
	        $inf->PostedTime 	= $row["f_time"];
	        $inf->deleted 		= (bool)$row["f_deleted"];
	        $ret = true;
	    }
	    return $ret;
	}
	
	static function Add(&$model/*FileInf*/)
	{
	    $se = new SqlExec();
	    $se->insert("up7_files"
	        , array("f_id"=>$model->id
            ,"f_sizeLoc"=>$model->sizeLoc
            ,"f_perSvr"=>$model->perSvr
            ,"f_complete"=>$model->complete
            ,"f_fdTask"=>$model->fdTask
            ,"f_fdChild"=>$model->fdChild
            ,"f_uid"=>$model->uid
            ,"f_nameLoc"=>$model->nameLoc
            ,"f_nameSvr"=>$model->nameSvr
            ,"f_pathLoc"=>$model->pathLoc
            ,"f_pathSvr"=>$model->pathSvr
            ,"f_pathRel"=>$model->pathRel
            ,"f_md5"=>$model->md5
            ,"f_lenLoc"=>$model->lenLoc
            ,"f_pid"=>$model->pid
            ,"f_pidRoot"=>$model->pidRoot
            ,"f_lenLocSec"=>$model->lenLocSec
            ,"f_blockCount"=>$model->blockCount
            ,"f_blockSize"=>$model->blockSize
            ,"f_blockPath"=>$model->blockPath
            ,"f_encrypt"=>$model->encrypt
	        ,"f_encryptAlgo"=>$model->encryptAlgorithm
	        ,"f_storage"=>$model->storage
	        ,"f_object_key"=>$model->objectKey
	        ,"f_object_url"=>$model->objectUrl
        ));
	}
	
	static function remove($id,$uid)
	{
		$sql = "update up7_files set f_deleted=1 where f_uid=:uid and f_id=:f_id";
		$db = new DbHelper();
		$cmd =& $db->GetCommand($sql);
	
		$cmd->bindParam(":uid", $uid);
		$cmd->bindParam(":f_id", $id);
		$db->ExecuteNonQuery($cmd);
	}
	
	static function delete($pathRel,$uid,$id)
	{
	    $sql = "update up7_files set f_deleted=1 where f_uid=:uid and f_pathRel=:pathRel and f_fdTask=0 and f_id!=:f_id";
	    
	    $db = new DbHelper();
	    $cmd =& $db->GetCommand($sql);
	    
	    $cmd->bindParam(":uid", $uid);
	    $cmd->bindParam(":pathRel", $pathRel);
	    $cmd->bindParam(":f_id", $id);
	    $db->ExecuteNonQuery($cmd);
	}
	
	static function del($uid,$id)
	{
	    $se = new SqlExec();
	    $se->update2("up7_files", array("f_deleted"=>1), array("f_id"=>$id,"f_uid"=>$uid));
	}
	
	static function all_uncmp($uid)
	{
		$sql = "select
				 f_id
				,f_fdTask
				,f_nameLoc
				,f_pathLoc
				,f_pathSvr
				,f_sizeLoc
				,f_perSvr
				from up7_files
				where f_uid=:uid and f_complete=0 and f_deleted=0;";
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$cmd->bindValue(":uid", $uid);
		$ret = $db->ExecuteDataSet($cmd);
		$files = array();
	
		foreach($ret as $row)
		{
			$f            = new FileInf();
			$f->id 		  = $row["f_id"];
			$f->fdTask    = (bool)$row["f_fdTask"];
			$f->nameLoc	  = $row["f_nameLoc"];
			$f->pathLoc   = $row["f_pathLoc"];
			$f->pathSvr   = $row["f_pathSvr"];
			$f->sizeLoc	  = $row["f_sizeLoc"];
			$f->perSvr	  = $row["f_perSvr"];
			$f->nameLoc   = PathTool::url_encode($f->nameLoc);
			$f->pathLoc   = PathTool::urlencode_path($f->pathLoc);
			$f->pathSvr   = PathTool::urlencode_path($f->pathSvr);
			$f->pathSvr   = str_replace("\\\\", "/", $f->pathLoc);
			$files[]      = $f;
		}
		$json = json_encode($files);
		$json = PathTool::url_decode($json);//还原汉字
		return $json;
	}
	
	function fd_scan($id,$uid)
	{
	    $sql = "update up7_files set f_scan=1 where f_id=:f_id and f_uid=:f_uid";
	    $db = new DbHelper();
	    $cmd =& $db->GetCommand($sql);
	    
	    $cmd->bindParam(":f_id", $id);
	    $cmd->bindValue(":f_uid", $uid);
	    $db->ExecuteNonQuery($cmd);
	}
	
	static function process($id,$perSvr,$lenSvr)
	{
		$sql = "update up7_files set f_lenSvr=:lenSvr,f_perSvr=:perSvr where f_id=:f_id";
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$cmd->bindParam(":lenSvr", $lenSvr);
		$cmd->bindParam(":perSvr", $perSvr);
		$cmd->bindParam(":f_id", $id);
		$db->ExecuteNonQuery($cmd);
	}
	
	static function existSameFile($name,$pid)
	{
	    $swm = new SqlWhereMerge();
	    $swm->equal("f_nameLoc", $name);
	    $swm->equal("f_pid", $pid);
	    $swm->equal("f_deleted", 0);
	
	    $sql = "select f_id from up7_files where " . $swm->to_sql();
	
	    $se = new SqlExec();
	    $arr = $se->exec_arr("up7_files", $sql, "f_id", "");
	    return count($arr) > 0;
	}
	
	static function updatePathRel($pathRelOld,$pathRelNew)
	{
	    $sql = "update up7_files set f_pathRel=REPLACE(f_pathRel,'$pathRelOld/','$pathRelNew/') where locate('$pathRelOld/',f_pathRel)=1";
	    
	    $se = new SqlExec();
	    $se->exe_sql($sql);
	}
	
	function addBatch(/*List<FileInf>*/ $fs)
	{
	    $sql = "insert into up7_files(".
	   	    " f_id".
	   	    ",f_pid".
	   	    ",f_pidRoot".
	   	    ",f_sizeLoc".
	   	    ",f_lenLoc".
	   	    ",f_lenSvr".
	   	    ",f_perSvr".
	   	    ",f_complete".
	   	    ",f_time".
	   	    ",f_deleted".
	   	    ",f_fdTask".
	   	    ",f_fdChild".
	   	    ",f_uid".
	   	    ",f_nameLoc".
	   	    ",f_nameSvr".
	   	    ",f_pathLoc".
	   	    ",f_pathSvr".
	   	    ",f_pathRel".
	   	    ",f_md5".
	   	    ",f_lenLocSec".
	   	    ",f_encrypt".
	   	    ",f_encryptAlgo".
	   	    ",f_storage".
	   	    ",f_object_key".
	   	    ",f_object_url".
	   	    ",f_blockSize".
	   	    ") values (".
	   	    " :f_id".
	   	    ",:f_pid".
	   	    ",:f_pidRoot".
	   	    ",:f_sizeLoc".
	   	    ",:f_lenLoc".
	   	    ",:f_lenSvr".
	   	    ",'100%'".
	   	    ",1".
	   	    ",:f_time".
	   	    ",0".
	   	    ",0".
	   	    ",1".
	   	    ",:f_uid".
	   	    ",:f_nameLoc".
	   	    ",:f_nameSvr".
	   	    ",:f_pathLoc".
	   	    ",:f_pathSvr".
	   	    ",:f_pathRel".
	   	    ",:f_md5".
	   	    ",:f_lenLocSec".
	   	    ",:f_encrypt".
	   	    ",:f_encryptAlgo".
	   	    ",:f_storage".
	   	    ",:f_object_key".
	   	    ",:f_object_url".
	   	    ",:f_blockSize".
	   	    ")";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sql);
	    
	    foreach($fs as $f)
	    {
	        $cmd->bindParam(":f_id", $f->id);
	        $cmd->bindParam(":f_pid", $f->pid);
	        $cmd->bindParam(":f_pidRoot", $f->pidRoot);
	        $cmd->bindParam(":f_sizeLoc", $f->sizeLoc);
	        $cmd->bindParam(":f_lenLoc", $f->lenLoc);
	        $cmd->bindParam(":f_lenSvr", $f->lenLoc);
	        $cmd->bindValue(":f_time", $f->PostedTime);
	        $cmd->bindParam(":f_uid", $f->uid);
	        $cmd->bindParam(":f_nameLoc", $f->nameLoc);
	        $cmd->bindParam(":f_nameSvr", $f->nameSvr);
	        $cmd->bindParam(":f_pathLoc", $f->pathLoc);
	        $cmd->bindParam(":f_pathSvr", $f->pathSvr);
	        $cmd->bindParam(":f_pathRel", $f->pathRel);
	        $cmd->bindParam(":f_md5", $f->md5);
	        $cmd->bindParam(":f_lenLocSec", $f->lenLocSec);
	        $cmd->bindParam(":f_encrypt", $f->encrypt,PDO::PARAM_BOOL);
	        $cmd->bindParam(":f_encryptAlgo", $f->encryptAlgorithm);
	        $cmd->bindParam(":f_storage", $f->storage);
	        $cmd->bindParam(":f_object_key", $f->objectKey);
	        $cmd->bindParam(":f_object_url", $f->objectUrl);
	        $cmd->bindParam(":f_blockSize", $f->blockSize);
	        
	        $db->ExecuteRow($cmd);
	    }
	}
}
?>